<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>无线弹窗</title>
</head>
<style>
    body {
        background-color: black;
    }

    .popup-area {
        position: fixed;
        top: 0;
        left: 0;
        right: 0;
        bottom: 0;
        background-color: rgb(33, 82, 106, .5);
        z-index: 1000; /* 默认层级 */
    }

    .popup {
        position: relative;
        left: 50%;
        top: 50%;
        transform: translate(-50%, -50%);
        max-width: 60%;
        min-width: 50%;
        max-height: 90%;
        min-height: 40%;
        padding: 20px;
        background-color: white;
    }

    .popup-content {
        position: relative;
    }

    .popup-buttons {
        position: absolute;
        bottom: 20px;
        right: 20px;
    }

    button {
        border-radius: 0;
        margin-left: 10px;
    }

    button:hover {
        background-color: black;
        color: white;
    }
</style>
<body>
<button onclick="openPopup()">打开弹窗</button>
</body>
<script>
    const popupManager = (function (zIndex) {
        //弹窗管理器
        class PopupManager {
            //构造器
            constructor(baseZIndex) {
                if (baseZIndex > 0) {
                    this.zIndexBase = baseZIndex;
                } else {
                    this.zIndexBase = 0;
                }
                this.popupStack = [];
            }

            //创建弹窗
            createPopup(options) {
                options.popupManager = this
                const popup = new Popup(options);
                const partOfZIndex = this.popupStack.push(popup);
                popup.setZIndex(this.zIndexBase + partOfZIndex);//显示层级由其在栈中的位置决定
                popup.show();
                return popup;
            }

            //关闭弹窗
            closePopup(popup) {
                this.popupStack.pop();
                popup.close();
            }
        }

        //弹窗
        class Popup {
            //构造器
            constructor(options) {
                this.options = options;
                this.options.operations.push({
                    innerText: '关闭',
                    onOperate: () => {
                        this.options.popupManager.closePopup(this);
                    }
                })
                this.element = this.createPopupElement();
                document.body.appendChild(this.element);
                this.operationsBinding();
            }

            //创建弹窗元素
            createPopupElement() {
                const element = document.createElement('div');
                element.classList.add('popup-area');
                element.innerHTML = `
                    <div class="popup">
                        <div class="popup-content">
                            <h3>${this.options.basic.title}</h3>
                            <p>${this.options.basic.content}</p>
                        </div>
                        <div class="popup-buttons"></div>
                    </div>`;
                return element;
            }

            //弹窗元素操作按钮事件绑定
            operationsBinding() {
                const buttonArea = this.element.querySelector('.popup-buttons');
                for (let operation of this.options.operations) {
                    if (operation.disabled !== true) {//不添加禁用状态的操作
                        const button = document.createElement('button');
                        button.classList.add('popup-button')
                        button.innerText = operation.innerText
                        button.addEventListener(operation.trigger ? operation.trigger : 'click', () => {
                            if (operation.onOperate) {
                                operation.onOperate(this.options.popupManager, this)
                            }
                        })
                        buttonArea.appendChild(button)
                    }
                }
            }

            //展示弹窗
            show() {
                this.element.style.display = 'block';
            }

            //关闭弹窗
            close() {
                this.element.remove();
            }

            //设置弹窗zIndex属性
            setZIndex(zIndex) {
                this.element.style.zIndex = zIndex
            }
        }

        //返回弹窗管理器实例
        return new PopupManager(zIndex);
    })(10);

    function openPopup() {
        // 创建第一个弹窗
        popupManager.createPopup({
            basic: {
                title: '温馨提示',
                content: '弹窗内容弹窗内容弹窗内容弹窗内容弹窗内容弹窗内容',
            },
            operations: [
                {
                    innerText: '下一步',
                    trigger: 'click',
                    onOperate: function (popupManager, popup) {
                        openAnotherPopup()
                    }
                },
                {
                    innerText: '返回',
                    disabled: true,
                    onOperate: function (popupManager, popup) {
                        popupManager.closePopup(popup)
                    }
                }
            ]
        });
    }

    function openAnotherPopup() {
        // 创建第一个弹窗
        popupManager.createPopup({
            basic: {
                title: '操作提示',
                content: `<p>弹窗内容弹窗内容弹窗内容弹窗内容</p>
                          <p>弹窗内容弹窗内容弹窗内容弹窗内容弹窗内容弹窗内容</p>
                          <p>弹窗内容弹窗内容弹窗内容弹窗内容弹窗内容弹窗内容</p>`
            },
            operations: [
                {
                    innerText: '下一步',
                    onOperate: function (popupManager, popup) {
                        openPopup();
                    }
                }
            ]
        });
    }
</script>
</html>